/**  IPV6工具类，处理IPV6验证，IPV6和IPV4的转换等工作  **/


//判断该ip是否合法
function isIp(ip){
	ip = ip.replace(/\s/g,"");
	if(isIpv6(ip) || isIpv4(ip) || isIpMix(ip)){
		return true;
	}
	return false;
}

//将ip转换为ipv6标准格式的十六进制数字
function ipToNorm(ip){
	ip = ip.replace(/\s/g,"");
	if(isIpv4(ip)){
		return ipv4ToSixteen(ip);
	}else if(isIpv6(ip)){
		return ipv6ToSixteen(ip);
	}else{
		return ipMixToSixteen(ip);
	}
}

//ipv6数字大小比较
function ipCompare(startIp,endIp){
	
}

//判断ip是否为标准ipv6地址
function isIpv6(ip){
	var reg = /^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$|^:((:[0-9a-fA-F]{1,4}){1,6}|:)$|^[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,5}|:)$|^([0-9a-fA-F]{1,4}:){2}((:[0-9a-fA-F]{1,4}){1,4}|:)$|^([0-9a-fA-F]{1,4}:){3}((:[0-9a-fA-F]{1,4}){1,3}|:)$|^([0-9a-fA-F]{1,4}:){4}((:[0-9a-fA-F]{1,4}){1,2}|:)$|^([0-9a-fA-F]{1,4}:){5}:([0-9a-fA-F]{1,4})?$|^([0-9a-fA-F]{1,4}:){6}:$/g;
	return reg.test(ip);
}

//判断混合ip前半部分是否为标准ipv6地址,此正则写的时候默认::最少占用32位(如：XXXX:XXXX)
function isShotIpv6(ip){
	var reg = /^::$|^([0-9a-fA-F]{1,4}:){0,3}[0-9a-fA-F]{1,4}::$|^(::)([0-9a-fA-F]{1,4}:){1,4}$|^([0-9a-fA-F]{1,4}:){6}$|^([0-9a-fA-F]{1,4})::([0-9a-fA-F]{1,4}:){1,3}$|^([0-9a-fA-F]{1,4}:[0-9a-fA-F]{1,4})::([0-9a-fA-F]{1,4}:){1,2}$|^([0-9a-fA-F]{1,4}:[0-9a-fA-F]{1,4}:[0-9a-fA-F]{1,4})::([0-9a-fA-F]{1,4}:)$/g;
	return reg.test(ip);
}

//判断ip是否为标准ipv4地址
function isIpv4(ip){
	var reg = /^(25[0-5]|2[0-4][0-9]|[1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$/g;
	return reg.test(ip);
}

//判断ip是否为混合ipv6地址
function isIpMix(ip){
	var sign = false;
	if(ip.lastIndexOf(":") == -1){
		return sign;
	}else{
		var i = ip.lastIndexOf(":");
		var v6 = ip.substring(0,i+1);
		var v4 = ip.substring(i+1,ip.length);
		if(isShotIpv6(v6)){
			sign = true;
			if(isIpv4(v4)){
				return sign;
			}else{
				return false;
			}
		}else{
			return sign;
		}
	}
}

//ipv4转换为十六进制ip地址
function ipv4ToSixteen(ip){
	var ips= new Array();
	ips=ip.split(".");
	for(i=0;i<ips.length;i++){
		var ipStr = "000"+ips[i];
		ips[i] = ipStr.substr(ipStr.length-3,ipStr.length);
	}
	ip = ips[0]+ips[1]+ips[2]+ips[3];
	var ipInt = parseInt(ip);
	ipInt = tenToSixteen(ipInt);
	
	ip = "00000000000000000000000000000000" + ipInt;
	ip = ip.substr(ip.length-32,ip.length);
	return ip;
}

//ipv6转换为十六进制ip地址
function ipv6ToSixteen(ip){
	
	if(ip.indexOf("::") == -1){
		var ips= new Array();
		ips=ip.split(":");
		for(i=0;i<ips.length;i++){
			var ipStr = "0000"+ips[i];
			ips[i] = ipStr.substr(ipStr.length-4,ipStr.length);
		}
		ip = ips[0]+ips[1]+ips[2]+ips[3]+ips[4]+ips[5]+ips[6]+ips[7];
	}else{
		var insertIndex = 0;
		var front = ip.substr(0,ip.indexOf("::"));
		var ips = new Array();
		insertIndex = front.length==0?0:front.split(":").length*4;
		var after = ip.substr(ip.indexOf("::")+2,ip.length);
		ips = front.split(":").concat(after.split(":"));
		
		for(i=0;i<ips.length;i++){
			var ipStr = "0000"+ips[i];
			ips[i] = ipStr.substr(ipStr.length-4,ipStr.length);
		}
		ip = "";
		for(i=0;i<ips.length;i++){
			ip += ips[i];
		}
		var supplement = "";
		for(i=0;i<8-ips.length;i++){
			supplement += "0000";
		}
		ip = insert_flg(ip,supplement,insertIndex);
	}
	
	
	return ip;
}

//混合ip转换为十六进制ip地址
function ipMixToSixteen(ip){
	var segmentation = ip.lastIndexOf(":");
	var v6 = ip.substr(0,segmentation+1)+"0000:0000";
	console.debug(v6);
	var v4 = ip.substr(segmentation+1,ip.length);
	console.debug(v4);
	
	v6 = ipv6ToSixteen(v6).substr(0,25);
	v4 = ipv4ToSixteen(v4).substr(25,32);
	ip = v6+v4;
	return ip;
}

//十六进制转化为十进制
function sixteenToTen(num){
	num.replace(/a/g,"10");
	num.replace(/b/g,"10");
	num.replace(/c/g,"10");
	num.replace(/d/g,"10");
	num.replace(/e/g,"10");
	num.replace(/f/g,"10");
}

//十进制转换为十六进制
function tenToSixteen(num){
	var rs = "";
	var temp;
	while(num/16 > 0){
		temp = num%16;
		rs = (temp+"").replace("10","a").replace("11","b").replace("12","c").replace("13","d").replace("14","e").replace("15","f") + rs;
		num = parseInt(num/16);
	}
	return rs;
}

//在字符串指定位置插入字符串
function insert_flg(str,flg,index){
	
    var start = str.substr(0,index);
    var end = str.substr(index,str.length);
    
    return start+flg+end;
}












